const http = require('http');
const fs = require('fs');

var page404 = fs.readFileSync('./www/404.html');
// 获取post数据
var getPost = function (req) {
	var str = '';
	req.on('data',function (data) {
		str += data;
	});
	req.on('end',function () {
		console.log(str);
	});
}
// 路由
var routeServer = function (req,res) {
	var file_name = './www' + req.url;
	// 目录默认是首页"index.html"
	if (req.url === '/')  file_name = './www/index.html';
	console.log(file_name);
	fs.readFile(file_name, function (err,data) {
		if (err) res.write(page404); 
		else res.write(data); 
		res.end();
	});	
}
var server = http.createServer(function (req,res) {
	// 路由分发
	routeServer(req,res);
	// 收取POST数据
	getPost(req);

});

server.listen(3000);
console.log("跑起来了");
